package com.example.nongye.utils;

import android.media.MediaPlayer;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import java.io.IOException;

/**
 * 音频播放
 *
 * @author Derek.Kidd
 * @version V2.0.0
 * @date 2022/3/23 15:10
 */
public class MediaPlayerUtil {

    private MediaPlayer mediaPlayer;
    private String path;
    private boolean isLooping = true;//是否循环模式
    private boolean isPause = false;//暂停状态

    private static volatile MediaPlayerUtil mediaPlayerUtil;

    private MediaPlayerUtil() {

    }

    public static MediaPlayerUtil getMediaPlayer() {
        if (mediaPlayerUtil == null) {
            synchronized (MediaPlayerUtil.class) {
                if (mediaPlayerUtil == null) {
                    mediaPlayerUtil = new MediaPlayerUtil();
                }
            }
        }
        return mediaPlayerUtil;
    }

    /**
     * 开始播放
     *
     * @param path
     */
    public void player(String path) {
        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Logger.d("播放完毕");
                    if (!isLooping) {
                        stopPlayer();
                    }
                }
            });
            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Logger.e("播放异常---> what = " + what + "extra = " + extra);
                    return false;
                }
            });
        }
        if (mediaPlayer.isPlaying()) {
            if (TextUtils.equals(this.path, path)) {
                Logger.d("同一语音停止播放");
                stopPlayer();
                return;
            }
        }
        try {
            this.path = path;
            stopPlayer();
            mediaPlayer.reset();
            mediaPlayer.setDataSource(path);
            mediaPlayer.setLooping(isLooping);//设置循环播放
            mediaPlayer.prepareAsync();//异步准备
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {//准备完成回调
                    mp.seekTo(0);//放在某种状态下，这里是prepared的接口函数下
                    mp.start();
                    Logger.d("开始播放");
                }

            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 暂停 | 继续
     */
    public void pauseOrPlayPlay() {
        if (null == mediaPlayer) {
            player(path);
        } else {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                isPause = true;
            } else if (isPause) {
                mediaPlayer.start();
                isPause = false;
            } else {
                player(path);
            }
        }
    }

    /**
     * 停止播放
     */
    public void stopPlayer() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }

    /**
     * 释放资源
     */
    public void release() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }
}
